//
//  NSDictionary+Extend.m
//  XLDoctor
//
//  Created by longma on 2018/12/27.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "UIViewController+MJUI.h"

@implementation UIViewController (MJUI)

+ (UIViewController *)mj_topViewController {
    UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    //循环之前tempVC和topVC是一样的
    UIViewController *tempVC = topVC;
    while (1) {
        if ([topVC isKindOfClass:[UITabBarController class]]) {
            topVC = ((UITabBarController*)topVC).selectedViewController;
        }
        if ([topVC isKindOfClass:[UINavigationController class]]) {
            topVC = ((UINavigationController*)topVC).visibleViewController;
        }
        if (topVC.presentedViewController) {
            topVC = topVC.presentedViewController;
        }
        //如果两者一样，说明循环结束了
        if ([tempVC isEqual:topVC]) {
            break;
        } else {
        //如果两者不一样，继续循环
            tempVC = topVC;
        }
    }
    return topVC;
}
- (void)mj_popViewControllerWaitTime{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJUIAwaitTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}
- (void)mj_popViewController{
    
    if(self.navigationController.viewControllers.count > 1){
        [self.navigationController popViewControllerAnimated:true];
    }else{
        [self dismissViewControllerAnimated:true completion:nil];
    }
}

- (void)mj_removeViewController:(UIViewController *)controller {
    MJUIAfter_GCD(0.1, ^{
        //移除控制器
        [self.rt_navigationController removeViewController:controller];
    });
}

- (void)mj_pushViewController:(NSString *)targetName {
    UIViewController *VC = [[NSClassFromString(targetName) alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)mj_animateAlongsideTransition:(void (^ __nullable)(id <UIViewControllerTransitionCoordinatorContext>context))animation
                             completion:(void (^ __nullable)(id <UIViewControllerTransitionCoordinatorContext>context))completion {
    if (self.transitionCoordinator) {
        BOOL animationQueuedToRun = [self.transitionCoordinator animateAlongsideTransition:animation completion:completion];
        // 某些情况下传给 animateAlongsideTransition 的 animation 不会被执行，这时候要自己手动调用一下
        // 但即便如此，completion 也会在动画结束后才被调用，因此这样写不会导致 completion 比 animation block 先调用
        // 某些情况包含：从 B 手势返回 A 的过程中，取消手势，animation 不会被调用
        // https://github.com/Tencent/QMUI_iOS/issues/692
        if (!animationQueuedToRun && animation) {
            animation(nil);
        }
    } else {
        if (animation) animation(nil);
        if (completion) completion(nil);
    }
}

@end

/// 语言
@implementation UIViewController (Language)

@dynamic mj_titleLocalizable;
- (void)setMj_titleLocalizable:(NSString *)mj_titleLocalizable{
    MJWeakSelf
    [RACObserve(LanguageHelper.shared, language) subscribeNext:^(NSString *language) {
        weakSelf.title = MJUILocalizedString(mj_titleLocalizable);
    }];

}
@end
